{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典集合的知识点及函数方法总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一、字典\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## （一）字典的相关概念："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在python中，字典是由一系列有着指向关系的键-值对（key=》value）组成的。如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "alien_0={'color':'green','points':'5'} #第一种示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Chinese': [79, 45, 99], 'math': [69, 43, 28]} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "grades_data={\n",
    "        'Chinese':[79,45,99],\n",
    "        'math':[69,43,28]\n",
    "}\n",
    "print(grades_data,type(grades_data))#第二种示例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字典键的特性：  \n",
    "\n",
    "字典值可以没有限制地取任何python对象，既可以是标准的对象，也可以是用户定义的，但键不行。  \n",
    "\n",
    "且不允许同一个键出现两次。创建时如果同一个键被赋值两次，后一个值会被记住,而键必须是唯一的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (二)字典的取值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n"
     ]
    }
   ],
   "source": [
    "#示例一\n",
    "#利用print函数依次指定字典名和放在方括号里的键，如：\n",
    "jiawei = {'age':'19'}\n",
    "print(jiawei['age'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jiawei\n",
      "None\n",
      "没有此键值对\n"
     ]
    }
   ],
   "source": [
    "#示例二\n",
    "#用get方法\n",
    "dic = {'name': 'jiawei', 'age': 19, 'hobby': 'dance'}\n",
    "print(dic['name']) #查看键 ，找不到会报错\n",
    "print(dic.get('name1')) #查看键，找不到返回None\n",
    "print(dic.get('name1','没有此键值对'))#可自定义值返回的内容，有则返回值，无则返回自定义值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## （三）字典的键-值对的基本操作\n",
    "- 1.添加键-值对\n",
    "- 2.修改键-值对\n",
    "- 3.删除键-值对\n",
    "- 4.字典的删除与覆盖"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.添加键-值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'age': '19', 'gender': 'female'}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "jiawei = {}\n",
    "jiawei['age']='19'\n",
    "jiawei['gender']='female'\n",
    "print(jiawei)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.修改键-值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jiawei is 19 years old.\n",
      "jiawei is 20 years old.\n"
     ]
    }
   ],
   "source": [
    "##示例一\n",
    "jiawei = {'age':'19','gender':'female'}\n",
    "print(\"jiawei is \"+jiawei['age']+\" years old.\")\n",
    "jiawei['age']='20'\n",
    "print(\"jiawei is \"+jiawei['age']+\" years old.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'birth_year': '1993', 'gender': 'male', 'new_song': 'criminal'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##示例二\n",
    "taemin ={'birth_year':'1993','gender':'male'}\n",
    "taemin\n",
    "\n",
    "taemin['new_song']='criminal'\n",
    "taemin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.删除键-值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'birth_year': '1993', 'gender': 'male', 'new_song': 'criminal'}\n",
      "{'birth_year': '1993', 'new_song': 'criminal'}\n"
     ]
    }
   ],
   "source": [
    "## 示例一 用del函数\n",
    "taemin ={'birth_year':'1993','gender':'male', 'new_song': 'criminal'}\n",
    "print(taemin)\n",
    "del taemin['gender']\n",
    "print(taemin)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "老男孩\n",
      "没有此值\n"
     ]
    }
   ],
   "source": [
    "##示例二 用pop方法\n",
    "dic = {'name': '老男孩','age': 56,'hobby': 'women',}\n",
    "print(dic.pop('name')) #删除键值对（返回删除的‘值’）\n",
    "print(dic.pop('aledd','没有此值')) #如果没有要删除的key，在字典里，则返回删除的value。没在字典里，则返回None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('hobby', 'women')\n",
      "{'age': 56}\n"
     ]
    }
   ],
   "source": [
    "##示例三 随机删除\n",
    "print(dic.popitem()) #随机删除，有返回值，返回值为元祖，里面是删除的键值对。\n",
    "print(dic)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.字典的删除与覆盖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "#删除字典 （利用clear方法）\n",
    "dic = {'name': '老男孩','age': 56,'hobby': 'women',}\n",
    "dic.clear()\n",
    "print(dic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jiawei', 'age': 19, 'sex': 'female'}\n",
      "{'name': 'jiawei', 'hobby': 'dance', 'age': 19, 'sex': 'female'}\n"
     ]
    }
   ],
   "source": [
    "# 覆盖字典\n",
    "dic = {\"name\": \"jiawei\", \"age\": 19, \"sex\": \"female\"}\n",
    "dic2 = {\"name\": \"jiayin\", \"hobby\":\"dance\" }\n",
    "dic2.update(dic) #将dic里面的所有键值对，覆盖添加到dic2中，dic不变。\n",
    "print(dic)\n",
    "print(dic2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## （四）遍历字典\n",
    "- 1.遍历所有的键-值对\n",
    "- 2.遍历字典中所有的键\n",
    "- 3.按顺序遍历字典中的所有键\n",
    "- 4.遍历字典中的所有值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.遍历所有的键-值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "key:age\n",
      "value:19\n",
      "\n",
      "key:weight\n",
      "value:78\n",
      "\n",
      "key:gender\n",
      "value:female\n"
     ]
    }
   ],
   "source": [
    "jiawei ={\n",
    "    'age':'19',\n",
    "    'weight':'78',\n",
    "    'gender':'female',\n",
    "}\n",
    "for key,value in jiawei.items():\n",
    "    print(\"\\nkey:\"+key) #\\n可确保在输出每个键-值时前都插入一个空行#\n",
    "    print(\"value:\"+value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jiawei's favorite idol is Taemin.\n",
      "Jiana's favorite idol is 戳爷.\n",
      "Jiayin's favorite idol is 赵丽颖.\n"
     ]
    }
   ],
   "source": [
    "favorite_idol ={\n",
    "    'jiawei':'taemin',\n",
    "    'jiana':'戳爷',\n",
    "    'jiayin':'赵丽颖',    \n",
    "}\n",
    "for name,idol in favorite_idol.items():\n",
    "    print(name.title()+\"'s favorite idol is \"+idol.title()+\".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.遍历字典中所有的键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jiawei\n",
      "Jiana\n",
      "Jiayin\n"
     ]
    }
   ],
   "source": [
    "favorite_idol ={\n",
    "    'jiawei':'taemin',\n",
    "    'jiana':'戳爷',\n",
    "    'jiayin':'赵丽颖', \n",
    "}\n",
    "for name in favorite_idol.keys(): ##用keys方法遍历键\n",
    "    print(name.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.按顺序遍历字典中的所有键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jiana thank you for coming.\n",
      "Jiawei thank you for coming.\n",
      "Jiayin thank you for coming.\n"
     ]
    }
   ],
   "source": [
    "##因为字典内print的结果并不一定按顺序，所以需要用sorted使打印结果按照顺序排列\n",
    "favorite_idol ={\n",
    "    'jiawei':'taemin',\n",
    "    'jiana':'戳爷',\n",
    "    'jiayin':'赵丽颖', \n",
    "}\n",
    "for name in sorted(favorite_idol.keys()):\n",
    "    print(name.title()+\" thank you for coming.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.遍历字典中的所有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The following idol have been mentioned\n",
      "Taemin\n",
      "戳爷\n",
      "赵丽颖\n"
     ]
    }
   ],
   "source": [
    "favorite_idol ={\n",
    "    'jiawei':'taemin',\n",
    "    'jiana':'戳爷',\n",
    "    'jiayin':'赵丽颖', \n",
    "}\n",
    "print(\"The following idol have been mentioned\")\n",
    "for idol in favorite_idol.values():##用values方法遍历值\n",
    "    print(idol.title())##title可以使打印出来首字母大写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## （五）字典的嵌套"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有时候，需要将一系列字典存储在列表中，或将列表作为值存储在字典中，这称为嵌套。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name_list': ['小红', '小明', '小蓝'], 'status': None, 'personal_msg': {'name': '巴拉啦小魔仙', 'age': 25, 'hobby_list': ['吃巧克力', '魔法', '画画']}}\n",
      "{'name_list': ['小红', '小明', '小蓝'], 'status': None, 'personal_msg': {'name': '巴拉啦小魔仙', 'age': 25, 'hobby_list': ['吃巧克力', '魔法', '画画']}, '学校': '魔仙堡'}\n",
      "{'name_list': ['小红', '小明', '小蓝'], 'status': True, 'personal_msg': {'name': '巴拉啦小魔仙', 'age': 25, 'hobby_list': ['吃巧克力', '魔法', '画画']}, '学校': '魔仙堡'}\n",
      "{'name_list': ['小红', '小明', '小蓝', '平平'], 'status': True, 'personal_msg': {'name': '巴拉啦小魔仙', 'age': 25, 'hobby_list': ['吃巧克力', '魔法', '画画']}, '学校': '魔仙堡'}\n",
      "{'name_list': ['小红', '小明', '小蓝', '平平'], 'status': True, 'personal_msg': {'name': '巴拉啦小魔仙', 'age': 25, 'hobby_list': ['吃巧克力', '魔法', '画画'], 'sex': '女'}, '学校': '魔仙堡'}\n",
      "{'name_list': ['小红', '小明', '小蓝', '平平'], 'status': True, 'personal_msg': {'name': '巴拉啦小魔仙', 'age': 25, 'hobby_list': ['吃巧克力', '魔法', '唱歌'], 'sex': '女'}, '学校': '魔仙堡'}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "dic = {\n",
    "    'name_list': ['小红', '小明', '小蓝'],\n",
    "    'status': None,\n",
    "    'personal_msg': {\n",
    "        'name': '巴拉啦小魔仙',\n",
    "        'age': 25,\n",
    "        'hobby_list': ['吃巧克力', '魔法', '画画'],\n",
    "    }\n",
    "}\n",
    "print(dic)\n",
    "# 1,给dic添加一个键值对\n",
    "dic['学校'] = '魔仙堡'\n",
    "print(dic)\n",
    "# 2，将status对应的值改成True\n",
    "dic['status'] = True\n",
    "print(dic)\n",
    "# 3，给name_list对应的列表追加一个元素：平平\n",
    "dic['name_list'].append('平平')\n",
    "print(dic)\n",
    "# 4，给personal_msg对应的字典添加一个键值对sex：女\n",
    "dic['personal_msg']['sex'] = '女'\n",
    "print(dic)\n",
    "# 5，将hobby_list 对应的列表中的吃巧克力改成吃炸鸡。\n",
    "dic['personal_msg']['hobby_list'][2] = '唱歌'\n",
    "print(dic)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## （六）字典的方法总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "[参考资料：Python3 字典 | 菜鸟教程](https://www.runoob.com/python3/python3-dictionary.html)\n",
    "## 字典中的内置函数：\n",
    "***\n",
    "|序号|函数|描述|\n",
    "|:---|---:|:--:|\n",
    "|1|len(dict)|计算字典元素个数，即键的总数。|\n",
    "|2|str(dict)|输出字典，以可打印的字符串表示。|\n",
    "|3|type(variable)|返回输入的变量类型，如果变量是字典就返回字典类型。|\n",
    "\n",
    "## 字典中的内置方法：\n",
    "|序号|方法|描述|\n",
    "|:---|---:|:--:|\n",
    "|1|dict.clear()|删除字典内所有元素|\n",
    "|2|dict.copy()|返回一个字典的浅复制|\n",
    "|3|dict.fromkeys()|创建一个新字典，以序列seq中元素做字典的键，val为字典所有键对应的初始值|\n",
    "|4|dict.get(key, default=None)|返回指定键的值，如果键不在字典中返回 default 设置的默认值|\n",
    "|5|key in dict|如果键在字典dict里返回true，否则返回false|\n",
    "|6|dict.items()|以列表返回可遍历的(键, 值) 元组数组|\n",
    "|7|dict.keys()|返回一个迭代器，可以使用 list() 来转换为列表|\n",
    "|8|dict.setdefault(key, default=None)|和get()类似, 但如果键不存在于字典中，将会添加键并将值设为default|\n",
    "|9|dict.update(dict2)|把字典dict2的键/值对更新到dict里|\n",
    "|10|dict.values()|返回一个迭代器，可以使用 list() 来转换为列表|\n",
    "|11|dict.pop(key[,default])|删除字典给定键 key 所对应的值，返回值为被删除的值。key值必须给出。 否则，返回default值。|\n",
    "|12|dict.popitem()|随机返回并删除字典中的最后一对键和值。|\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 二、集合\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## （一）集合的相关概念："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "集合（set）是一个无序的不重复元素序列。\n",
    "可以使用大括号 { } 或者 set() 函数创建集合.  \n",
    "\n",
    "注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典。  \n",
    "\n",
    "创建示例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'w', 'h', '_', 'a', 'i', 'u', 'n', 'z', 'g', 'e', 'j'}\n"
     ]
    }
   ],
   "source": [
    "a=set(\"zhuang_jiawei\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## （二）集合与元素的基本操作：\n",
    "- 1.添加元素\n",
    "- 2.删除元素\n",
    "- 3.计算集合中元素的个数\n",
    "- 4.清空集合\n",
    "- 5.判断元素是否在集合中存在"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'xiaohong', 'jiayin', 'jiawei', 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "## 示例一 用add方法\n",
    "student_name = set((\"xiaohong\", \"xiaoming\", \"jiawei\"))\n",
    "student_name.add(\"jiayin\")\n",
    "print(student_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'jiayin', 1, 3, 'jiawei', 'xiaoming', 'xiaohong'}\n",
      "{1, 3, 4, 5, 6, 'xiaohong', 'jiayin', 'jiawei', 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "## 示例二：也可以用update的方法添加元素，且参数可以是列表，元组，字典等\n",
    "student_name.update({1,3})\n",
    "print(student_name)\n",
    "\n",
    "student_name.update([1,4],[5,6])  \n",
    "print(student_name)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 5, 6, 'xiaohong', 'jiayin', 'xiaoming'}\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'xiaolan'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-8c32e4d19520>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mstudent_name\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"jiawei\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudent_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mstudent_name\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"xiaolan\"\u001b[0m\u001b[1;33m)\u001b[0m   \u001b[1;31m# remove不存在的元素的话会发生错误\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'xiaolan'"
     ]
    }
   ],
   "source": [
    "## 示例一 用remove方法\n",
    "student_name.remove(\"jiawei\")\n",
    "print(student_name)\n",
    "student_name.remove(\"xiaolan\")   # remove不存在的元素的话会发生错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 5, 6, 'xiaohong', 'jiayin', 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "##示例二 用discard方法，即使要移除的元素不存在，也不会运行报错\n",
    "student_name.discard(\"xiaozi\")\n",
    "print(student_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3, 4, 5, 6, 'xiaohong', 'jiayin', 'xiaoming'}\n"
     ]
    }
   ],
   "source": [
    "## 示例三 利用pop方法随机删除\n",
    "student_name.pop()\n",
    "print(student_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.计算集合中元素的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#示例 用len函数计算\n",
    "student_name = set((\"xiaohong\", \"xiaoming\", \"jiawei\"))\n",
    "len(student_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.清空集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set()\n"
     ]
    }
   ],
   "source": [
    "# 利用clear方法清空\n",
    "student_name = set((\"xiaohong\", \"xiaoming\", \"jiawei\"))\n",
    "student_name.clear()\n",
    "print(student_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.判断元素是否在集合中存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n"
     ]
    }
   ],
   "source": [
    "#示例① 利用if语句\n",
    "student_name = set((\"xiaohong\", \"xiaoming\", \"jiawei\"))\n",
    "if 'xiaohong'in student_name:\n",
    "    print(\"true\")\n",
    "else:\n",
    "     print(\"false\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 示例② 简易版\n",
    "student_name=set((\"xiaohong\", \"xiaoming\", \"jiawei\"))\n",
    "\"jiawei\"in student_name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (三)集合的运算\n",
    "- 1.交集\n",
    "- 2.并集\n",
    "- 3.补集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'h', 'n'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用inersection方法找出两个集合之间重合的元素\n",
    "a=set(\"zhuangjiawei\")\n",
    "b=set(\"python\")\n",
    "c=a.intersection(b)\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'1', '3', '新', '班', '网'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用union方法找出两个集合之间的总和元素\n",
    "a=set(\"网新3班\")\n",
    "b=set(\"网新1班\")\n",
    "c=a.union(b)\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.补集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'1', '2'}\n",
      "{'5', '4'}\n"
     ]
    }
   ],
   "source": [
    "#用difference方法找出一个集合有而另一个集合没有的元素，需要注意调用difference方法的时候的前后集合顺序，顺序不同，运行结果和意义也不同。\n",
    "a=set((\"1\",\"2\",\"3\"))\n",
    "b=set((\"3\",\"4\",\"5\"))\n",
    "c=a.difference(b)#c表示a有b没有的元素\n",
    "print(c)\n",
    "\n",
    "e=b.difference(a)#e表示b有a没有的元素\n",
    "print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## （四）集合的方法总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 参考资料：[python3集合|菜鸟教程 ](https://www.runoob.com/python3/python3-set.html)\n",
    "|序号|方法|描述|\n",
    "|:---|---:|:--:|\n",
    "|1|set.add()|为集合添加元素|\n",
    "|2|set.clear()|移除集合中的所有元素|\n",
    "|3|set.copy()|拷贝一个集合|\n",
    "|4|set.difference()|返回多个集合的差集|\n",
    "|5|set.difference_update()|移除集合中的元素，该元素在指定的集合也存在。|\n",
    "|6|set.discard()|删除集合中指定的元素|\n",
    "|7|set.intersecrion()|返回集合的交集|\n",
    "|8|set.intersection_update()|返回集合的交集|\n",
    "|9|ste.isdisjoint()|判断两个集合是否包含相同的元素，如果没有返回 True，否则返回 False。|\n",
    "|10|set.issubset()|判断指定集合是否为该方法参数集合的子集。|\n",
    "|11|set.issuperset()|判断该方法的参数集合是否为指定集合的子集|\n",
    "|12|set.pop()|随机移除元素|\n",
    "|13|set.remove()|移除制定元素|\n",
    "|14|set.symmetric_difference()|返回两个集合中不重复的元素集合。|\n",
    "|15|set.symmetric_difference_update()|移除当前集合中在另外一个指定集合相同的元素，并将另外一个指定集合中不同的元素插入到当前集合中。|\n",
    "|16|set.union()|返回两个集合的并集|\n",
    "|17|set.update()|给集合添加元素|\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
